#!/usr/bin/perl -wc

package RFCMail::DNS::BlockList::Query;

use strict;
use Carp qw(croak);
use RFCMail::DNS::DNSUtils qw(filtered_dns_lookup);


sub new {
    my ($class, $fqdn) = @_;
    my $self = bless { a_status => "OK", txt_status => "OK" }, $class;
    $self->{a_records} = [eval { map($_->address, filtered_dns_lookup($fqdn, "A")) }];
    if ($@) { ($self->{a_status}) = $@ =~ /(.*)/ }
    $self->{txt_records} = [eval { map($_->rdatastr, filtered_dns_lookup($fqdn, "TXT")) }];
    if ($@) { ($self->{txt_status}) = $@ =~ /(.*)/ }
    return ($self);
}


sub by_ip {
    my ($class, $bl, $ip) = @_;
    $ip =~ /\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/
	or croak("$ip is not an IPv4 address");
    return ($class->new("$4.$3.$2.$1.$bl"));
}


sub by_rhs {
    my ($class, $bl, $rhs) = @_;
    return ($class->new("$rhs.$bl"));
}


sub summary {
    my ($self) = @_;
    return ("A_STATUS: ".$self->a_status." (".join(", ", $self->a_records).")",
	    "TXT_STATUS: ".$self->txt_status, $self->txt_records,
	   );
}


sub a_status { $_[0]->{a_status} }

sub txt_status { $_[0]->{txt_status} }

sub a_records { @{$_[0]->{a_records}} }

sub txt_records { @{$_[0]->{txt_records}} }


1;
